package com.iss.itemplate.controller;

import com.iss.itemplate.dto.AppDto;
import com.iss.itemplate.dto.CaptchaDto;
import com.iss.itemplate.dto.ErrorCode;
import com.iss.itemplate.dto.JsonResponseDto;
import com.iss.itemplate.service.HelloService;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * **********************************************************************
 * <p/>
 * iServiceSoft Inc © - 2009 All Rights Reserved
 * _____________________________________________
 * <p/>
 * NOTICE:  All information contained herein is, and remains
 * the property of iServiceSoft. Intellectual and technical concepts
 * contained herein are proprietary of iServiceSoft and may be covered
 * by U.S. and Foreign Patents, patents in process, and are protected
 * by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from iServiceSoft Incorporated.
 * <p/>
 * ***********************************************************************
 */

@Controller
public class HelloController {
   @Autowired
   private HelloService helloService;

   @RequestMapping(value = "/hello/getName", method = RequestMethod.GET)
   public @ResponseBody String getAppName(HttpServletRequest request) {
      return helloService.getAppName();
   }

   @RequestMapping(value = "/hello/getAppInfo", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto<AppDto> getApp(HttpServletRequest request) {
      AppDto app = helloService.getApp();
      return new JsonResponseDto<AppDto>(app, ErrorCode.SUCCESS);
   }
   
   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/hello/insertAppInfo", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto insertAppInfo(HttpServletRequest request, @RequestBody AppDto appDto) {
      helloService.insertApp(appDto);
      return new JsonResponseDto(ErrorCode.SUCCESS);
   }
   
   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/hello/updateAppInfo", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto updateAppInfo(HttpServletRequest request, @RequestBody AppDto appDto) {
      helloService.updateApp(appDto);
      return new JsonResponseDto(ErrorCode.SUCCESS);
   }
   
   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/hello/deleteAppInfo/{appId}", method = RequestMethod.GET)
   public @ResponseBody
   JsonResponseDto deleteAppInfo(HttpServletRequest request, @PathVariable Long appId) {
      helloService.deleteApp(appId);
      return new JsonResponseDto(ErrorCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/hello/recaptcha", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<String> testRecaptcha(HttpServletRequest request, @RequestBody CaptchaDto captchaDto) {
      String remoteAddr = request.getRemoteAddr();
      ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
      reCaptcha.setPrivateKey("6Lcx9P4SAAAAAJccBqdw1bDEmuRJ1RxHrdTzlDuc");

//      String challenge = request.getParameter("recaptcha_challenge_field");
//      String uresponse = request.getParameter("recaptcha_response_field");
      String challenge = captchaDto.getChallengeField();
      String uresponse = captchaDto.getResponseField();
      ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
            remoteAddr, challenge, uresponse);

      if (reCaptchaResponse.isValid()) {
         return new JsonResponseDto("CAPTCHA Validation Success",ErrorCode.SUCCESS);
      } else {
         return new JsonResponseDto("CAPTCHA Validation Failed! Try Again.",ErrorCode.SUCCESS);
      }
   }
}
